<?php

namespace app\blog\model;
use think\Model;
use think\Db;

class GoodspecModel extends Model{
    //显示规格列表
    public  function session()
    {
        $id=cmf_get_current_admin_id();
        $where['agency_id']=$id;
        $session=DB::name('shop')->where($where)->find();
        return $session;
    }
    public function goodspeclist($param){
        $session=$this->session();
        $where['f.shop_id']=$session['id'];
        if($param)
        {
            $where['f.f_name']=array('like','%'.$param.'%');
        }
        $data = DB::name("format")->alias('f')->join('shop s','s.id =f.shop_id  ')->where($where)->field('f.*,s.shop_name')->paginate(10);
        if($data->toArray()) {
            foreach ($data as $k => $v) {
                $data->toArray();
                $date = array();
                $date = $v;
                if ($v['status'] == 1) {
                    $date['status_name'] = '启用';
                } else {
                    $date['status_name'] = '禁用';
                }
                $data->offsetSet($k, $date);
            }
        }
        return $data;
    }
    /*
     * 添加规格
     * guid()生成唯一标识 在common
     * $data包含(name,status)
     */
    public function goodspecadd($data){
            $session=$this->session();
            $log = array();
            $log['for_guid'] = guid();
            $log['f_name'] = $data['name'];
            $log['status'] =1;
            $log['shop_id'] = $session['id'];
            $log['c_time'] = time();
            $result = Db::name('format')->insert($log);
            return $result;
    }
    /*
     * 修改规格
     * $data包含(name)
     */
    public function goodspecupdate($where,$data){
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $result = Db::name("format")->where($where)->update($data);
        return $result;
    }
    /*
     * 删除规格
     * $data包含(id)
     */
    public function goodspecdelete($cause){
        $session=$this->session();
        $where['shop_id']=$session['id'];
        $where['g_format']=$cause['f_id'];
        $cause['shop_id']=$session['id'];
        $goods=DB::name('goods')->where($where)->field('g_id')->select()->toArray();
        $goods_id=array();
        foreach($goods as $k=>$v)
        {
            $goods_id[$k]=$v['g_id'];
        }
        $goods_id=implode(',',$goods_id);
        if($goods_id !=null)
        {
            $order=DB::name('order_detail')->where('goods_id','in',$goods_id)->find();
            if($order)
            {
                $status['num']=0;
                $status['con']='类别下有订单，不能删除！';
            }else{
                $goodsclass=DB::name('format')->delete($cause);
                if($goodsclass)
                {
                    $status['num']=1;
                    $status['con']='删除成功';
                }else{
                    $status['num']=0;
                    $status['con']='删除失败！';
                }
            }
        }else{
            $goodsclass=DB::name('format')->delete($cause);
            if($goodsclass)
            {
                $status['num']=1;
                $status['con']='删除成功';
            }else{
                $status['num']=0;
                $status['con']='删除失败！';
            }
        }
        return $status;
    }
    /**
     * 根据规格id获取指定的规格
    */
    public function getGone($where){
        $session=$this->session();
        $where['shop_id']=$session['id'];
        return Db::name("format")->where($where)->find();
    }
}

